/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package au.csiro.nt.psdp.model;

/**
 *
 * @author Jingyu ZHANG
 */
import org.acegisecurity.GrantedAuthority;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.appfuse.model.BaseObject;

import javax.persistence.*;
import java.io.Serializable;


@Entity
@Table(name = "role")
public class Role extends BaseObject implements Serializable, GrantedAuthority
{
    private static final long serialVersionUID = 3690197650654049848L;
    private Long id;
    private String name;
    private String description;

    public Role()
    {
    }

    public Role(String name)
    {
        this.name = name;
    }

    @Id
    @Column(name = "role_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId()
    {
        return id;
    }

    /**
     * @see org.acegisecurity.GrantedAuthority#getAuthority()
     */
    @Transient
    public String getAuthority()
    {
        return getName();
    }

    @Column(length = 20)
    public String getName()
    {
        return this.name;
    }

    @Column(length = 64)
    public String getDescription()
    {
        return this.description;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (!(o instanceof Role)) return false;

        final Role role = (Role) o;

        return !(name != null ? !name.equals(role.name) : role.name != null);

    }

    public int hashCode()
    {
        return (name != null ? name.hashCode() : 0);
    }

    public String toString()
    {
        return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
                .append(this.name)
                .toString();
    }

}
